package pl.edu.agh.gronoMining.resultAnalyzer.resultComparer;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import pl.edu.agh.gronoMining.frequentSubgraphFinder.frequentSearcher.subgraph.DFSCode;
import pl.edu.agh.gronoMining.resultAnalyzer.resultComparer.parser.ToDFSParser;
import pl.edu.agh.gronoMining.resultAnalyzer.resultComparer.parser.ToDFSParserImpl;

public class Main {

private static final Set<String> possibleArgsSet = new HashSet<String>();
	
	private static final Set<String> neceseryArgsSet = new HashSet<String>();
	
	private static final String LOGGER_PROP_FILE = "resources/logger.properties.xml";
	
	static {
		possibleArgsSet.add("file1");
		possibleArgsSet.add("file2");
		possibleArgsSet.add("outputFile");
	}
	
	private static Properties parseArgs(String[] args) throws ParseException {
		Properties p = new Properties();
		for (String arg : args) {
			String[] argParts = arg.split("=");
			if (argParts.length != 2) {
				throw new ParseException("wrong argument "+arg, 1);
			} else if (argParts[0].charAt(0) != '-') {
				throw new ParseException("wrong argument "+arg, 1);
			} else {
				String key = argParts[0].substring(1);
				String value = argParts[1];
				if (possibleArgsSet.contains(key))
					p.setProperty(key, value);
				else
					throw new ParseException("wrong argument "+arg, 1);
			}
		}
		return p;
	}
	
	
	public static void main(String[] args) throws ParseException, IOException {
		
		DOMConfigurator.configure(LOGGER_PROP_FILE);
		Logger logger = Logger.getLogger(Main.class);
		logger.info("loger inicialized");
		
		Properties prop = parseArgs(args);
		if (! prop.containsKey("outputFile"))
			prop.setProperty("outputFile", "comparation.results.txt");
		
		ToDFSParser parser = new ToDFSParserImpl();
		
		File graphFile1 = new File(prop.getProperty("file1"));
		File graphFile2 = new File(prop.getProperty("file2"));
		File outputFile = new File(prop.getProperty("outputFile"));
		outputFile.createNewFile();
		
		logger.info("try to parse file "+graphFile1);
		Map<Integer, DFSCode> graphs1 = parser.parse(graphFile1);
		logger.info("parsing complete - found "+graphs1.size()+" graphs");
		
		logger.info("try to parse file "+graphFile2);
		Map<Integer, DFSCode> graphs2 = parser.parse(graphFile2);
		logger.info("parsing complete - found "+graphs2.size()+" graphs");
		
		Map <DFSCode, Integer> DFSToIdMap1 = new HashMap<DFSCode, Integer>();
		for (Integer i : graphs1.keySet()) {
			DFSToIdMap1.put(graphs1.get(i), i);
		}
		
		Map <DFSCode, Integer> DFSToIdMap2 = new HashMap<DFSCode, Integer>();
		for (Integer i : graphs2.keySet()) {
			DFSToIdMap2.put(graphs2.get(i), i);
		}
		
		
		PrintWriter writer = new PrintWriter(new FileWriter(outputFile));
		
		writer.println("GRAPH 1 [" + graphFile1 + "]");
		
		Set<Message> messages = new HashSet<Message>();
		for (DFSCode dfs : DFSToIdMap1.keySet()) {
			int id = DFSToIdMap1.get(dfs);
			Integer graphs2Id = DFSToIdMap2.get(dfs);
			if (graphs2Id == null) {
				messages.add(new Message(id, null));
			} else {
				messages.add(new Message(id, graphs2Id));
			}
		}
		Object[] messagesArray = messages.toArray();
		Arrays.sort(messagesArray);
		for (Object s : messagesArray)
			writer.println(s);
			
		writer.println();
		writer.println("GRAPH 2 [" + graphFile2 + "]");
		
		messages = new HashSet<Message>();
		for (DFSCode dfs : DFSToIdMap2.keySet()) {
			int id = DFSToIdMap2.get(dfs);
			Integer graphs1Id = DFSToIdMap1.get(dfs);
			if (graphs1Id == null) {
				messages.add(new Message(id, null));
			} else {
				messages.add(new Message(id, graphs1Id));
			}
		}
		messagesArray = messages.toArray();
		Arrays.sort(messagesArray);
		for (Object s : messagesArray)
			writer.println(s);
		
		writer.flush();
		logger.info("result saved to "+outputFile);
	}
	
}
